package com.hncc.kumao.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hncc.kumao.biz.IBreakpointUploadTaskBiz;
import com.hncc.kumao.biz.INetdiskAccessTokenBiz;
import com.hncc.kumao.biz.IOfflineDownloadTaskBiz;
import com.hncc.kumao.biz.IUserBiz;
import com.hncc.kumao.entity.BreakpointUploadTask;
import com.hncc.kumao.entity.OfflineDownloadTask;
import com.hncc.kumao.entity.User;
import com.hncc.kumao.util.ConstValue;
import com.opensymphony.xwork2.ActionContext;

public class AdminAction extends BaseAction {

    //用户业务层对象
    private IUserBiz userBiz;
    //下载任务业务层对象
    private IOfflineDownloadTaskBiz downloadBiz;
    //上传任务业务层对象
    private IBreakpointUploadTaskBiz uploadBiz;
    //网盘信息业务层对象
    private INetdiskAccessTokenBiz netdiskBiz;
    
    /**
     * 后台登录
     */
    public void login() {
        try {
            String requestData = (String) getRequest().getAttribute(ConstValue.REQUEST_DATA);
            System.out.println(requestData);
            JSONObject jsonObject = JSON.parseObject(requestData);
            String username = jsonObject.getString("username");
            String password = jsonObject.getString("password");
            if("admin".equals(username) && "admin".equals(password)) {
                //将用户名存入Session
                ActionContext.getContext().getSession().put("adminUser", username);
                //返回信息到页面
                jsonReturn(true, ConstValue.RESULT_CODE_SUCCESS, "登录成功");
            } else {
                //用户名或密码错误
                jsonReturn(false, ConstValue.RESULT_CODE_BAD_REQUEST, "用户名或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonReturn(false, ConstValue.RESULT_CODE_SERVER_ERROR, "服务器好像出了点问题");
        }
    }
    
    /**
     * 判断是否登录
     */
    public void showName() {
        //获取emp对象
        String name = (String) ActionContext.getContext().getSession().get("adminUser");
        //判断对象是否为空(session超时或用户未登录直接访问主页面)
        if(name != null) {
            jsonReturn(true, ConstValue.RESULT_CODE_SUCCESS, name);
        } else {
            jsonReturn(false, ConstValue.RESULT_CODE_BAD_REQUEST, "还未登录");
        }
    }
    
    /**
     * 退出登录
     */
    public void logout() {
        //移除seeion中的对象
        ActionContext.getContext().getSession().remove("adminUser");
    }
    
    /**
     * 获取用户列表
     */
    public void userList() { 
        try {
            //调用业务层获取列表
            List<User> userList = userBiz.getList(null, null, null);
            List<User> listClone = new ArrayList<User>();
            for (User user : userList) {
                User userClone = (User) user.clone();
                userClone.setHeadPortraitUrl(ConstValue.BASE_RES_URL + user.getHeadPortraitUrl());
                listClone.add(userClone);
            }
            Map map=new HashMap();
            map.put("rows", listClone);
            map.put("total", listClone.size());
            //返回数据到页面
            jsonReturn(true, ConstValue.RESULT_CODE_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            jsonReturn(false, ConstValue.RESULT_CODE_SERVER_ERROR, "服务器好像出了点问题");
        }
    }

    private int page;//页码
    private int rows;//每页记录数

    /**
     * 配合EasyUI分页获取用户列表
     */
    public void userListByPageWeb() {
        try {
            System.out.println("page: " + page + ", rows: " + rows);
            int firstResult=(page-1)*rows;
            List<User> list = userBiz.getListByPage(null, null, null, firstResult, rows);
            List<User> listClone = new ArrayList<User>();
            for (User user : list) {
                User userClone = (User) user.clone();
                userClone.setHeadPortraitUrl(ConstValue.BASE_RES_URL + user.getHeadPortraitUrl());
                listClone.add(userClone);
            }
            long count = userBiz.getCount(null, null, null);       
            Map map=new HashMap();
            map.put("rows", listClone);
            map.put("total", count);
            String jsonString = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
            
            getResponse().getWriter().write(jsonString);
        } catch (Exception e) {
            jsonReturn(false, ConstValue.RESULT_CODE_SERVER_ERROR, "服务器好像出了点问题");
            e.printStackTrace();
        }
    }
    
    /**
     * 配合EasyUI分页获取下载任务列表
     */
    public void downloadListByPageWeb() {
        
    }
    
    /**
     * 各网盘任务类型统计
     */
    public void netdiskTaskTypeInfo() {
        try {
            //下载任务数据
            int dBaiduNum = 0;
            int dJGYNum = 0;
            int dFTPNum = 0;
            int dSeafileNum = 0;
            int dSMBNum = 0;
            //上传任务数据
            int uBaiduNum = 0;
            int uJGYNum = 0;
            int uFTPNum = 0;
            int uSeafileNum = 0;
            int uSMBNum = 0;
            //先获取下载任务列表
            List<OfflineDownloadTask> downloadTasks = downloadBiz.getList(null, null, null);
            for (OfflineDownloadTask task : downloadTasks) {
                switch (task.getNetdiskAccessToken().getNetdiskType()) {
                case ConstValue.NETDISK_TYPE_BAIDU:
                    dBaiduNum++;
                    break;
                case ConstValue.NETDISK_TYPE_JIANGUOYUN:
                    dJGYNum++;
                    break;
                case ConstValue.NETDISK_TYPE_FTP:
                    dFTPNum++;
                    break;
                case ConstValue.NETDISK_TYPE_SEAFILE:
                    dSeafileNum++;
                    break;
                case ConstValue.NETDISK_TYPE_SMB:
                    dSMBNum++;
                    break;
                }
            }
            //再获取上传任务列表
            List<BreakpointUploadTask> uploadTasks = uploadBiz.getList(null, null, null);
            for (BreakpointUploadTask task : uploadTasks) {
                switch (task.getNetdiskAccessToken().getNetdiskType()) {
                case ConstValue.NETDISK_TYPE_BAIDU:
                    uBaiduNum++;
                    break;
                case ConstValue.NETDISK_TYPE_JIANGUOYUN:
                    uJGYNum++;
                    break;
                case ConstValue.NETDISK_TYPE_FTP:
                    uFTPNum++;
                    break;
                case ConstValue.NETDISK_TYPE_SEAFILE:
                    uSeafileNum++;
                    break;
                case ConstValue.NETDISK_TYPE_SMB:
                    uSMBNum++;
                    break;
                }
            }
            List<Integer> dTaskTypeList = new ArrayList<Integer>();
            dTaskTypeList.add(dBaiduNum);
            dTaskTypeList.add(dJGYNum);
            dTaskTypeList.add(dFTPNum);
            dTaskTypeList.add(dSeafileNum);
            dTaskTypeList.add(dSMBNum);
            List<Integer> uTaskTypeList = new ArrayList<Integer>();
            uTaskTypeList.add(uBaiduNum);
            uTaskTypeList.add(uJGYNum);
            uTaskTypeList.add(uFTPNum);
            uTaskTypeList.add(uSeafileNum);
            uTaskTypeList.add(uSMBNum);
            Map<String, List<Integer>> resultMap = new HashMap<String, List<Integer>>();
            resultMap.put("downloadData", dTaskTypeList);
            resultMap.put("uploadData", uTaskTypeList);
            jsonReturn(true, ConstValue.RESULT_CODE_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            jsonReturn(false, ConstValue.RESULT_CODE_SERVER_ERROR, "服务器好像出了点问题");
        }
    }
    
    /**
     * 获取指定类型任务的状态统计
     */
    public void taskStateInfo() {
        try {
            //从Request中获取数据(这个数据来自拦截器)
            Integer type;
            String jsonString = (String) getRequest().getAttribute(ConstValue.REQUEST_DATA);
            if(jsonString != null && jsonString.trim().length() > 0) {
                JSONObject jsonObject = JSON.parseObject(jsonString);
                type = jsonObject.getInteger("type");                
            } else {
                type = 1;
            }
            
            List list;
            
            if(type == 1) {
                //获取下载任务数据
                list = downloadBiz.getTaskStateInfo();
                System.out.println(list);
            } else {
                //获取上传任务数据
                list = uploadBiz.getTaskStateInfo();
                System.out.println(list);
            }
            jsonReturn(true, ConstValue.RESULT_CODE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            jsonReturn(false, ConstValue.RESULT_CODE_SERVER_ERROR, "服务器好像出了点问题");
        }
    }
    
    
    public void setUserBiz(IUserBiz userBiz) {
        this.userBiz = userBiz;
    }
    public void setDownloadBiz(IOfflineDownloadTaskBiz downloadBiz) {
        this.downloadBiz = downloadBiz;
    }
    public void setUploadBiz(IBreakpointUploadTaskBiz uploadBiz) {
        this.uploadBiz = uploadBiz;
    }
    public void setNetdiskBiz(INetdiskAccessTokenBiz netdiskBiz) {
        this.netdiskBiz = netdiskBiz;
    }

    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
}
